/**
 * 
 */
package com.base.myproject.client.busi.b2c;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;

/**
 * @author zgw
 *2009-7-14 下午09:37:45
 * javazgw@gmail.com
 */
public class Gcode extends BaseJDBCPanel{
	static BaseStore gcodestore  = null;
	static BaseGrid gcodeegrid =null;
	
	static BaseGrid attrgroupgrid = null;

	public Gcode() {
		super();
		init();
	}

	public void init() {

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	this.add(initWeiget(),centerData);

	}
	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
//		TabPanel tabpanel = new TabPanel();
//
//		tabpanel.setAutoWidth(true);
//	
//		tabpanel.setAutoHeight(true);
//		TabItem normal = new TabItem("商品分类");
//	
//		normal.add(CreateGcode());
//		
//		
//		
//		tabpanel.add(normal);

		TableLayout tl = new TableLayout(2);
		
		panel.setLayout(tl);
		TableData td = new TableData();
		td.setColspan(1);
		td.setRowspan(4);
		td.setVerticalAlign(VerticalAlignment.TOP);
		TableData td1 = new TableData();
		td1.setColspan(1);
		td1.setRowspan(1);
		td1.setVerticalAlign(VerticalAlignment.TOP);
		TableData td2 = new TableData();
		td2.setColspan(1);
		td2.setRowspan(1);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		panel.add(CreateGcode(),td);
		panel.add(CreateGcodeAttrgroup(),td1);
		panel.add(CreateGcodeAttr(),td2);
		
		 setMaincomponent(gcodeegrid);
			registerMainStore(gcodestore);
			this.registerStore(gcodestore);

		return panel;
	}

	private ContentPanel CreateGcode()
	{
		ContentPanel panel = new ContentPanel();
//		panel.setSize(400, 400);
		panel.setAutoHeight(true);
		panel.setAutoWidth(true);
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig gcode = new ColumnConfig("gcode", "商品大类编码", 100);
		TextField<String> gcodetext = new TextField<String>();
		gcode.setEditor(new CellEditor(gcodetext));
		columns.add(gcode);
		ColumnConfig gname = new ColumnConfig("gname", "商品大类名称", 100);
		TextField<String> gnametext = new TextField<String>();
		gname.setEditor(new CellEditor(gnametext));
		columns.add(gname);
		
		ColumnConfig attrcode = new ColumnConfig("attrcode", "商品大类属性", 100);
		TextField<String> attrcodetext = new TextField<String>();
		attrcode.setEditor(new CellEditor(attrcodetext));
		columns.add(attrcode);

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("gcode");
		type.addField("gname");
		type.addField("attrcode");

		StoreInfo info = new StoreInfo();

		info.setTablename("gcode");
		info.setKeyname(new String[]{"gcode"});
		info.setSql("select * from gcode");
//		info.setParent(store);// 关联主表
//		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
//		info.setLinkparentcolumns(new String[] { "ccode" }); // 关联主表字段
		 gcodestore = new BaseStore<JDBCModelData>(type, info);
//		gudong.setName("gudong");
//		gudong.setKeyname(new String[] { "ccode", "idx" }); // 主键
		ColumnModel cm = new ColumnModel(columns);
		 gcodeegrid = new BaseGrid(gcodestore, cm);

		cbs.setSelectionMode(SelectionMode.SINGLE);

		gcodeegrid.setSelectionModel(cbs);
		gcodeegrid.setAutoHeight(true);
		gcodeegrid.setAutoWidth(true);
		gcodeegrid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener(){

			@Override
			public void selectionChanged(SelectionChangedEvent se) {
				if(se.getSelection().size()>0)
				{
//					attrgroupgrid.getModel()
					attrgroupgrid.getSelectionModel().deselectAll();
				//	int attr = ((BaseModelData)gcodeegrid.getSelectionModel().getSelectedItems().get(0)).get("attrcode");
				//	System.out.println("size="+attrgroupgrid.getStore().getModels().size());
				//	for(int i=0;i<((BaseModelData)attrgroupgrid.getModel()).getProperties().size())
					
					
				int size = attrgroupgrid.getStore().getModels().size();
				int attr =Integer.parseInt(((BaseModelData)gcodeegrid.getSelectionModel().getSelectedItems().get(0)).get("attrcode").toString());
				
				for(int i=0;i<size;i++)
				{
					int attrcode = Integer.parseInt(attrgroupgrid.getValue(i, "attrcode").toString());
					if((attrcode & attr) == attrcode)
					{
						attrgroupgrid.getSelectionModel().select(i, true);
					}
					
				}
					
				}
				else
				{
					attrgroupgrid.getSelectionModel().deselectAll();
				}
				
			}

			});

		registerStore(gcodestore);
		panel.add(gcodeegrid);
		panel.setScrollMode(Scroll.AUTO);
		return panel;
	}
	private ContentPanel CreateGcodeAttrgroup()
	{
		ContentPanel panel = new ContentPanel();
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel(){
			 public void deselectAll() {
				    doDeselect(new ArrayList(selected), true);
				  }
		};
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig attr = new ColumnConfig("attrcode", "商品属性", 100);
		TextField<String> attrtext = new TextField<String>();
		attr.setEditor(new CellEditor(attrtext));
		columns.add(attr);
		
		ColumnConfig attrname = new ColumnConfig("attrname", "商品属性", 100);
//		TextField<String> attrnametext = new TextField<String>();
//		attrname.setEditor(new CellEditor(attrnametext));
		columns.add(attrname);
		

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("attrcode");
		type.addField("attrname");

		StoreInfo info = new StoreInfo();

		info.setTablename("b2cgcodeattrgroup");
		
		info.setSql("select * from b2cgcodeattrgroup");
//		info.setParent(store);// 关联主表
//		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
//		info.setLinkparentcolumns(new String[] { "ccode" }); // 关联主表字段
		BaseStore<JDBCModelData> gcodeattrgroup = new BaseStore<JDBCModelData>(type, info);
//		gudong.setName("gudong");
//		gudong.setKeyname(new String[] { "ccode", "idx" }); // 主键
		ColumnModel cm = new ColumnModel(columns);
		attrgroupgrid = new BaseGrid(gcodeattrgroup, cm);

		cbs.setSelectionMode(SelectionMode.MULTI);

		attrgroupgrid.setSelectionModel(cbs);
		attrgroupgrid.setAutoHeight(true);
		attrgroupgrid.setAutoWidth(true);
		
		
		
		attrgroupgrid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener(){

			@Override
			public void selectionChanged(SelectionChangedEvent se) {
				
			
				
//				System.out.println("----"+gcodeegrid.getSelectionModel().getSelectedItems().size());
				if(gcodeegrid.getSelectionModel().getSelectedItems().size()<1 && attrgroupgrid.getSelectionModel().getSelectedItems().size()>0)
				{
					
					MessageBox.alert("警告", "请先选择商品大类！否则属性将不能正确设置。", null);
					return;
					
				}
				
				if(gcodeegrid.getSelectionModel().getSelectedItems().size()<1 )
				{
					return ;
				}
				int size = se.getSelection().size();
				 int z=0;
				for(int i=0;i<size;i++)
				{
//				System.out.println("--"+((BaseModelData)se.getSelection().get(i)).get("attrcode"));
				z = z | Integer.parseInt(((BaseModelData)se.getSelection().get(i)).get("attrcode").toString());
				}
				
				System.out.println("z="+z);
				//((BaseModelData)gcodeegrid.getSelectionModel().getSelectedItems().get(0)).set("attrcode", z);
				int index = 0;
				if(gcodeegrid.getSelectionModel().getSelectedItems().size()>0)
				 index = gcodestore.indexOf((BaseModelData)gcodeegrid.getSelectionModel().getSelectedItems().get(0));
				if(index>=0)
				gcodestore.setValue(index, "attrcode", z);
				else
				{
					MessageBox.alert("警告", "未找到指定的行！", null);
				}
			
			}

		
			
		});
		
//		registerStore(gcodeattrgroup); 不参与保存，所以不注册
		panel.add(attrgroupgrid);
		
		return panel;
		
	}
	/**
	 * 商品大类的属性
	 * @return
	 */
	private ContentPanel CreateGcodeAttr()
	{
		ContentPanel panel = new ContentPanel();
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig gcode = new ColumnConfig("gcode", "商品大类", 100);
		TextField<String> gcodetext = new TextField<String>();
		gcode.setEditor(new CellEditor(gcodetext));
		columns.add(gcode);
		
		ColumnConfig qty = new ColumnConfig("qty", "商品数量", 100);
		TextField<String> qtytext = new TextField<String>();
		qty.setEditor(new CellEditor(qtytext));
		columns.add(qty);
		
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "商品数量单位", 100);
		TextField<String> qtyunittext = new TextField<String>();
		qtyunit.setEditor(new CellEditor(qtyunittext));
		columns.add(qtyunit);
		
		ColumnConfig discount = new ColumnConfig("discount", "折扣比例", 100);
		TextField<String> discounttext = new TextField<String>();
		discount.setEditor(new CellEditor(discounttext));
		columns.add(discount);
	/*	
		ColumnConfig attr = new ColumnConfig("attr", "商品属性", 60);
		TextField<String> attrtext = new TextField<String>();
		attr.setEditor(new CellEditor(attrtext));
		columns.add(attr);
		
		ColumnConfig attr2 = new ColumnConfig("attr2", "商品属性", 60);
		TextField<String> attr2text = new TextField<String>();
		attr2.setEditor(new CellEditor(attr2text));
		columns.add(attr2);
		ColumnConfig attr3 = new ColumnConfig("attr3", "商品属性", 60);
		TextField<String> attr3text = new TextField<String>();
		attr3.setEditor(new CellEditor(attr3text));
		columns.add(attr3);
		ColumnConfig attr4 = new ColumnConfig("attr4", "商品属性", 60);
		TextField<String> attr4text = new TextField<String>();
		attr4.setEditor(new CellEditor(attr4text));
		columns.add(attr4);
		ColumnConfig attr5 = new ColumnConfig("attr5", "商品属性", 60);
		TextField<String> attr5text = new TextField<String>();
		attr5.setEditor(new CellEditor(attr5text));
		columns.add(attr5);
		ColumnConfig attr6 = new ColumnConfig("attr6", "商品属性", 60);
		TextField<String> attr6text = new TextField<String>();
		attr6.setEditor(new CellEditor(attr6text));
		columns.add(attr6);
		
		ColumnConfig attr7 = new ColumnConfig("attr7", "商品属性", 60);
		TextField<String> attr7text = new TextField<String>();
		attr7.setEditor(new CellEditor(attr7text));
		columns.add(attr7);
		
		ColumnConfig attr8 = new ColumnConfig("attr8", "商品属性", 60);
		TextField<String> attr8text = new TextField<String>();
		attr8.setEditor(new CellEditor(attr8text));
		columns.add(attr8);*/

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("gcode");
		type.addField("qty");
		type.addField("qtyunit");
		type.addField("discount");
	
/*		type.addField("attr1");
		type.addField("attr2");
		type.addField("attr3");
		type.addField("attr4");
		type.addField("attr5");
		type.addField("attr6");
		type.addField("attr7");
		type.addField("attr8");*/
		

		StoreInfo info = new StoreInfo();

		info.setTablename("b2cgcodeattr");
		
		info.setSql("select gcode,qty,qtyunit,discount from b2cgcodeattr");
		info.setParent(gcodestore);// 关联主表
		info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		info.setLinkparentcolumns(new String[] { "gcode" }); // 关联主表字段
		info.setKeyname(new String[] { "gcode", "idx" });
		BaseStore<JDBCModelData> gcodeattr = new BaseStore<JDBCModelData>(type, info);
//		gudong.setName("gudong");
//		gudong.setKeyname(new String[] { "ccode", "idx" }); // 主键
		ColumnModel cm = new ColumnModel(columns);
		BaseGrid attrgrid = new BaseGrid(gcodeattr, cm);

		cbs.setSelectionMode(SelectionMode.SINGLE);

		attrgrid.setSelectionModel(cbs);
		attrgrid.setAutoHeight(true);
		//attrgrid.setAutoWidth(true);

		registerStore(gcodeattr);
		attrgrid.setAutoHeight(true);
		panel.add(attrgrid);
	
		panel.setScrollMode(Scroll.AUTO);
		return panel;
		
	}
	
}
